package com.zhs.gccs.activity.gccs;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.KeyboardUtils;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.http.Api;
import com.zhs.gccs.http.ProgressObserver;
import com.zhs.gccs.model.GccsProjectSsgh;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.vo.ApplyGccsTask;
import com.zhs.gccs.vo.GccsDataExtAddBean;
import com.zhs.gccs.vo.GccsExtDataBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;

public class GccsZjbfFormActivity extends BaseActivity {
    public static String intent_data = "data";
    public static String intent_dataExt ="dataExt";
    @BindView(R.id.iv_back)
    View iv_back;
    @BindView(R.id.btn_back)
    View btn_back;
    @BindView(R.id.tv_save)
    View tv_save;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;


    @BindView(R.id.et_jhztz)
    EditText et_jhztz;
    @BindView(R.id.et_pszbje)
    EditText et_pszbje;
    @BindView(R.id.et_zbje)
    EditText et_zbje;
    @BindView(R.id.et_bfje)
    EditText et_bfje;
    @BindView(R.id.et_jsje)
    EditText et_jsje;
    @BindView(R.id.et_jyje)
    EditText et_jyje;
    @BindView(R.id.radio_1)
    RadioGroup radio_1;//结余资金使用方向
    @BindView(R.id.et_zjsyfx)
    EditText et_zjsyfx;
    @BindView(R.id.radio_2)
    RadioGroup radio_2;//是否按期完工
    ApplyGccsTask taskBean;
    GccsProjectSsgh gccsProjectSsgh;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gccs_zjbf_form);
//        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
//        ImmersionBar.with(this)
//                .statusBarColor(R.color.main_title_pressed_bg)
//                .statusBarDarkFont(false).init();
        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
        ButterKnife.bind(this);
        llRoot.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);
        Intent intent = getIntent();
        if(intent.hasExtra(intent_data)){
            taskBean = (ApplyGccsTask) intent.getExtras().get(intent_data);
        }
        initView();
        if(taskBean!=null){
            getGccsFormData();
        }
    }

    private void initView() {
        iv_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                KeyboardUtils.hideSoftInput(GccsZjbfFormActivity.this);
                finish();
            }
        });
        btn_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                KeyboardUtils.hideSoftInput(GccsZjbfFormActivity.this);
                finish();
            }
        });
        tv_save.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                saveData();
            }
        });
    }

    private void getGccsFormData() {
        showDialog();
        toSubscribeSingleOb(Api.getDefault().
                        getGccsExtForm(taskBean.getPid()),
                new ProgressObserver(this, false) {
                    @Override
                    protected void _onNext(String result) {
                        dismissDialog();
                        GccsExtDataBean bean = new Gson().fromJson(result, GccsExtDataBean.class);//GsonImpl.get().toObject(result, SmTaskBean.class);
                        if (bean != null && bean.getCode() == 200
                                && bean.getData() != null) {
                            gccsProjectSsgh = bean.getData();
                            initData();
                        } else {

                        }
                    }

                    @Override
                    protected void _onError(String result) {
                        dismissDialog();
                    }
                });
    }

    private void initData() {
        if(gccsProjectSsgh==null)return;
        if(!TextUtils.isEmpty(gccsProjectSsgh.getZjbfJhztz())){
            et_jhztz.setText(gccsProjectSsgh.getZjbfJhztz());
        }else {
            et_jhztz.setText("");
        }
        if(!TextUtils.isEmpty(gccsProjectSsgh.getZjbfPsje())){
            et_pszbje.setText(gccsProjectSsgh.getZjbfPsje());
        }else {
            et_pszbje.setText("");
        }
        if(!TextUtils.isEmpty(gccsProjectSsgh.getZjbfZbje())){
            et_zbje.setText(gccsProjectSsgh.getZjbfZbje());
        }else {
            et_zbje.setText("");
        }
        if(!TextUtils.isEmpty(gccsProjectSsgh.getZjbfBfje())){
            et_bfje.setText(gccsProjectSsgh.getZjbfBfje());
        }else {
            et_bfje.setText("");
        }
        if(!TextUtils.isEmpty(gccsProjectSsgh.getZjbfJsje())){
            et_jsje.setText(gccsProjectSsgh.getZjbfJsje());
        }else {
            et_jsje.setText("");
        }
        if(!TextUtils.isEmpty(gccsProjectSsgh.getZjbfJyje())){
            et_jyje.setText(gccsProjectSsgh.getZjbfJyje());//结余资金
        }else {
            et_jyje.setText("");
        }
        if("1".equals(gccsProjectSsgh.getZjbfJyjesyfx())){
            radio_1.check(R.id.radio_1_1);
        }else if("0".equals(gccsProjectSsgh.getZjbfJyjesyfx())){
            radio_1.check(R.id.radio_1_2);
        }
        if(!TextUtils.isEmpty(gccsProjectSsgh.getZjbfjyjesyfxBz())){
            et_zjsyfx.setText(gccsProjectSsgh.getZjbfjyjesyfxBz());
        }else {
            et_zjsyfx.setText("");
        }
        if("1".equals(gccsProjectSsgh.getZjbfSfaqwg())){
            radio_2.check(R.id.radio_2_1);
        }else if("0".equals(gccsProjectSsgh.getZjbfSfaqwg())){
            radio_2.check(R.id.radio_2_2);
        }
    }

    private void saveData() {
        KeyboardUtils.hideSoftInput(this);
        showDialog();
        Map hashMap = new HashMap<>();
        hashMap.put("pid",taskBean.getPid());
        if(!TextUtils.isEmpty(et_jhztz.getText().toString())){
            hashMap.put("zjbfJhztz",et_jhztz.getText().toString().trim());
        }else {
            hashMap.put("zjbfJhztz","");
        }
        if(!TextUtils.isEmpty(et_pszbje.getText().toString())){
            hashMap.put("zjbfPsje",et_pszbje.getText().toString().trim());
        }else {
            hashMap.put("zjbfPsje","");
        }
        if(!TextUtils.isEmpty(et_zbje.getText().toString())){
            hashMap.put("zjbfZbje",et_zbje.getText().toString().trim());
        }else {
            hashMap.put("zjbfZbje","");
        }
        if(!TextUtils.isEmpty(et_bfje.getText().toString())){
            hashMap.put("zjbfBfje",et_bfje.getText().toString().trim());
        }else {
            hashMap.put("zjbfBfje","");
        }
        if(!TextUtils.isEmpty(et_jsje.getText().toString())){
            hashMap.put("zjbfJsje",et_jsje.getText().toString().trim());
        }else {
            hashMap.put("zjbfJsje","");
        }
        //结余资金
        if(!TextUtils.isEmpty(et_jyje.getText().toString())){
            hashMap.put("zjbfJyje",et_jyje.getText().toString().trim());
        }else {
            hashMap.put("zjbfJyje","");
        }
        //结余资金使用方向
        if (radio_1.getCheckedRadioButtonId() == R.id.radio_1_1) {
            hashMap.put("zjbfJyjesyfx","1");
        } else if (radio_1.getCheckedRadioButtonId() == R.id.radio_1_2){
            hashMap.put("zjbfJyjesyfx","0");
        }
        if(!TextUtils.isEmpty(et_zjsyfx.getText().toString())){
            hashMap.put("zjbfjyjesyfxBz",et_zjsyfx.getText().toString().trim());
        }else {
            hashMap.put("zjbfjyjesyfxBz","");
        }
        if (radio_2.getCheckedRadioButtonId() == R.id.radio_2_1) {
            hashMap.put("zjbfSfaqwg","1");
        } else if (radio_2.getCheckedRadioButtonId() == R.id.radio_2_2){
            hashMap.put("zjbfSfaqwg","0");
        }
        RequestBody requestBody = createRequestBody(hashMap);
        toSubscribeSingleOb(Api.getDefault().saveGccsExtform(requestBody),
                new ProgressObserver(this, false) {
                    @Override
                    protected void _onNext(String result) {
                        GccsDataExtAddBean bean = new Gson().
                                fromJson(result, GccsDataExtAddBean.class);
                        if (bean != null && bean.getCode() == 200) {
                            ToastUtils.toast("添加成功");
//                            setResult(RESULT_OK);
//                            finish();
                        }else {
                            ToastUtils.toast("添加失败，"+bean.getMsg());
                        }
                        dismissDialog();
                    }
                    @Override
                    protected void _onError(String result) {
                        ToastUtils.toast("添加失败");
                        dismissDialog();
                    }
                });
    }


}
